前言
在上一篇文章中,我们通过一系列操作生成了我们想要的数据,证明了数据的准确性,那么我们在获取到小程序的加密数据后,首先做的是校验数据,校验通过后进行数据的解密,也就是今天这篇文章
新建小程序用户隐私数据解密工具类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
   | 
 
 
  public class WXBizDataUtil {       public static String illegalAesKey = "50001";     public static String illegalIv = "50002";     public static String illegalBuffer = "50003";     public static String decodeBase64Error = "50004";      public static String noData = "50005";        private String appid;       private String sessionKey;       public WXBizDataCrypt(String appid, String sessionKey) {         this.appid = appid;         this.sessionKey = sessionKey;     }       
 
 
 
 
 
      public String decryptData(String encryptedData, String iv) {         if (StringUtils.length(sessionKey) != 24) {             return illegalAesKey;         }                  byte[] aesKey = Base64.decodeBase64(sessionKey);           if (StringUtils.length(iv) != 24) {             return illegalIv;         }                  byte[] aesIV = Base64.decodeBase64(iv);                    byte[] aesCipher = Base64.decodeBase64(encryptedData);         try {             byte[] resultByte = AESUtil.decrypt(aesCipher, aesKey, aesIV);             if (null != resultByte && resultByte.length > 0) {                 String userInfo = new String(resultByte, "UTF-8");                 JSONObject jsons = JSON.parseObject(userInfo);                 String id = jsons.getJSONObject("watermark").getString("appid");                 if (!StringUtils.equals(id, appid)) {                     return illegalBuffer;                 }                 return userInfo;             } else {                 return noData;             }         } catch (InvalidAlgorithmParameterException e) {             e.printStackTrace();         } catch (UnsupportedEncodingException e) {             e.printStackTrace();         }         return null;     }   }
 
  | 
 
在controller相关方法里调用
1 2 3 4 5 6 7 8 9 10 11
   | private JSONObject getUserInfo(String iv, String encryptedData, String sessionKey) {         String userInfo = null;                  try {             WXBizDataCrypt biz = new WXBizDataCrypt(CommonConfig.appletAppID, sessionKey);             userInfo = biz.decryptData(encryptedData, iv);         } catch (Exception e) {             e.printStackTrace();         }         return JSONObject.parseObject(userInfo);     }
  | 
 
获取到的数据就是 我们需要的数据,次数据包含了用户的隐私数据,不建议乱传,毕竟泄露用户隐私数据是 违法行为  
结语
至此,微信获取用户数据的流程基本上完成,剩下的就跟微信没啥关系了